package com.bieyang.anjia.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.bieyang.anjia.R;
import com.bieyang.anjia.module.NewsInfo;
import com.bieyang.anjia.sqlitedatabase.AnjiaSQLiteOpenHelper;
import com.bieyang.anjia.sqlitedatabase.SqliteOperation;
import com.bieyang.anjia.utils.Constants;
import com.bieyang.anjia.utils.SharedPrefsUtil;
import com.bieyang.anjia.utils.Show;

/**
 * 查看更多资讯
 * */
public class AcFindNewsDetail extends BaseActivity {
	Button btnBack;
	RelativeLayout rlYuShe, newsDetail;
	Context con;
	List<NewsInfo> newsList;
	WebView webNews;
	String webUrl;
	SqliteOperation sqlOperation;
	public static ArrayList<String> urlList = new ArrayList<String>();
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case Constants.GET_NEWS_SUCESS:
				newsList = sqlOperation.queryNews("isdelete=?",
						new String[] { "F" }, null);
				// newsList=msg.getData().getParcelable("newsInfo");
				// Log.d("newsList", newsList.toString());
				Show.toast(AcFindNewsDetail.this, R.string.tip_get_news_ok);
				break;
			case Constants.GET_NEWS_FAIL:
				Show.toast(AcFindNewsDetail.this, R.string.tip_get_news_fail);
				break;

			default:
				break;
			}
		};

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.find_news_detail);
		sqlOperation = new SqliteOperation(con, AnjiaSQLiteOpenHelper.dbName,
				AnjiaSQLiteOpenHelper.version);
		con = AcFindNewsDetail.this;
		NewsInfo info = (NewsInfo) getIntent().getSerializableExtra("newsInfo");
		if (null != info) {
			webUrl = info.webUrl;
			new SqliteOperation(con, AnjiaSQLiteOpenHelper.dbName,
					AnjiaSQLiteOpenHelper.version).update("newsinfo",
					"newsid = ?", new String[] { info.newsId + "" }, "isnew",
					"F");
		}
		initView();

	}

	@SuppressLint("SetJavaScriptEnabled")
	private void initView() {

		OnClick onClick = new OnClick();
		btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(onClick);

		// String url="http://www.baidu.com";

		webNews = (WebView) findViewById(R.id.webNews);
		webNews.getSettings().setJavaScriptEnabled(true);

		Intent intent = getIntent();
		if (intent.getBooleanExtra("formNotification", false)) {
			String notifyUrl = SharedPrefsUtil
					.getStringValue(con, "weburl", "");
			if (!"".equals(notifyUrl)) {
				webNews.loadUrl(notifyUrl);
				webNews.setWebViewClient(new MyWebViewClient());
				return;
			}
		}

		if ("".equals(webUrl)) {
			initData();
		}
		int curIndex = getIntent().getIntExtra("intentNews", -1);
		try {
			// webNews.loadUrl("http://" + urlList.get(curIndex));
			webNews.loadUrl(urlList.get(curIndex));			 
		} catch (Exception e) {
			// TODO: handle exception
		}
		webNews.loadUrl(webUrl);
		webNews.setWebViewClient(new MyWebViewClient());

	}

	/**
	 * 页面内跳转
	 * */
	public boolean shouldOverrideUrlLoading(WebView view, String url) {

		if (url.indexOf("tel:") < 0) {// 页面上有数字会导致连接电话
			view.loadUrl(url);
		}
		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	void initData() {
		App.netManager.getNewsMore(con, handler,
				sqlOperation.queryColumn("newsinfo", "alarmtime", null, null));
	}

	class OnClick implements android.view.View.OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnBack:
				finish();
				break;
			default:
				break;
			}

		}

	}


	// Web视图
	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}

}